package jdbc.Dao;

import jdbc.domain.User;
import jdbc.test.JDBCTest;
import jdbc.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/12 0012.
 */
public class UserDao {//x 为输入语句 用来为下面的 执行做基础
    public void handle(String x) throws SQLException {
    //   Connection conn = JDBCTest.getconnection();
      Connection conn = JDBCUtils.getconnection(); //接受 包装类的信息 到conn
        Statement statement =conn.createStatement();//connection不能被执行  所以要创建一个执行方法
        statement.executeUpdate(x);//执行更新
    }
    public void Checke() throws SQLException {
        Connection conn=JDBCUtils.getconnection();
        Statement statement=conn.createStatement();
        String sql="select * from user";
        ResultSet set=statement.executeQuery(sql);
        while (set.next()){
            System.out.println(set.getInt("1")+","+set.getString("2"));
        }
    }



    public List<User> findAll(){

        Connection conn = JDBCUtils.getconnection();

        ResultSet set=null;

        List<User> list=new ArrayList<>() ;
        Statement statement=null;
        try {
             statement= conn.createStatement();
            String sql="select * from user";
             set=statement.executeQuery(sql);
            while (set.next()){
                User user=new User();
                user.setId(set.getInt(1));
                user.setUsername(set.getString(2));
                user.setPassword(set.getInt(3));
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            if(set!=null){
                try {
                    set.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return  null;
    }
    public boolean insert(User user){
        Connection conn =JDBCUtils.getconnection();
        String sql="insert into (id,username,password,email,phone) values(?,?,?,?,?)";
        PreparedStatement ps=null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,user.getId());
            ps.setString(2,user.getUsername());
            ps.setInt(3,user.getPassword());
            ps.setString(4,user.getEmail());
            ps.setInt(5,user.getPhone());
           int num= ps.executeUpdate();
           if(num ==1){
               return true;
           }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return true;
    }
    public boolean delete(){
        return false;
    }
    public boolean updata(){
        return false;
    }
    public User findById(int id){
        Connection conn = JDBCUtils.getconnection();
    User user= new User();
        ResultSet set=null;
   //    Statement statement=null;
        PreparedStatement ps= null;
        try {
         //   statement= conn.createStatement();
            String sql="select * from user where id = ?";
            ps =conn.prepareStatement(sql);
            ps.setInt(1,id);
           set =ps.executeQuery(sql);
            while (set.next()){
                user.setId(set.getInt( 1));
                user.setUsername(set.getString(2));
                user.setPassword(set.getInt(3));
                return  user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            if(set!=null){
                try {
                    set.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return  null;
    }
}
